#!/bin/bash
#####################################################
# Name: changeNginxUpstream.sh
# Version: V1.1
# Author: Feng
# Description: modify nginx upstream
# Create Date: 2018-03-07
# Email:
#####################################################

sysName=$1
gray_pool_ip_lists=$2
main_pool_ip_lists=$3
default_pool_ip_lists=$2,$3
gray_pool_name=${sysName}_gray
main_pool_name=${sysName}_main
default_pool_name=${sysName}_default
confDir=/srv/salt/prod/nginx/files/nginx-config-dir/vhosts_net
ngx_upstream_file=$confDir/${sysName}.conf

#check if pool exist in nginx config file
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s*${pool_name}[^-]" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m the ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m the ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#show pool content in nginx config file
function show_pool_in_ngx_upstream() {
    pool_name_first_line=`egrep -n "upstream\s*${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`
    line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`
    pool_name_end_line=${pool_name_first_line}
    for line in ${line_list[*]};do
        if [ $line -gt ${pool_name_first_line} ];then
            pool_name_end_line=${line}
            break;
        fi
    done
    sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file}
}

#add pool to upstream config file
function add_pool_to_upstream() {
    pool_ip=`awk 'BEGIN{list="'${pool_ip_lists}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`
    for ip in ${pool_ip[*]};do
        echo "add ${pool_name} ${ip} in ${ngx_upstream_file}"
        sed -i "/upstream\s*${pool_name}[^-]*{/a\    server ${ip};" ${ngx_upstream_file}
    done
    echo -e "\033[31m ====Adding complete:==== \033[0m"
}

#delete all server in upstream config file
function delete_ip_from_upstream() {
    pool_name_first_line=`egrep -n "upstream\s*${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`
    line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`
    pool_name_end_line=${pool_name_first_line}
    for line in ${line_list[*]};do
        if [ $line -gt ${pool_name_first_line} ];then
            pool_name_end_line=${line}
            break;
        fi
    done
    #get lines of pool
    line_count=`sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file} | wc -l`
    #couldn't delete if lines less than 3
    if [ ${line_count} -eq 3 ];then
        echo -e "\033[31m this is lowest configure. \033[0m"
    fi
    for ((i=${pool_name_first_line};i<=${pool_name_end_line};i++));do
        sed -i -r "${i}s/\s*server\s*[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,};//ig" ${ngx_upstream_file}
    done
    sed -i '/^$/d' ${ngx_upstream_file}
    echo -e "\033[31m ====Deleting complete:==== \033[0m"
}

echo -e "\033[31m ====Change GRAY group:==== \033[0m"
pool_name=${gray_pool_name}
pool_ip_lists=${gray_pool_ip_lists}

check_pool_in_ngx_upstream;
show_pool_in_ngx_upstream;
delete_ip_from_upstream;
show_pool_in_ngx_upstream;
add_pool_to_upstream;
show_pool_in_ngx_upstream;

echo -e "\033[31m ====Change MAIN group:==== \033[0m"
pool_name=${main_pool_name}
pool_ip_lists=${main_pool_ip_lists}

check_pool_in_ngx_upstream;
show_pool_in_ngx_upstream;
delete_ip_from_upstream;
show_pool_in_ngx_upstream;
add_pool_to_upstream;
show_pool_in_ngx_upstream;

echo -e "\033[31m ====Change DEFAULT group:==== \033[0m"
pool_name=${default_pool_name}
pool_ip_lists=${default_pool_ip_lists}

check_pool_in_ngx_upstream;
show_pool_in_ngx_upstream;
delete_ip_from_upstream;
show_pool_in_ngx_upstream;
add_pool_to_upstream;
show_pool_in_ngx_upstream;


salt -N 'ngxonline' state.highstate
salt -N 'ngxonline' cmd.run 'nginx -t'
salt -N 'ngxonline' cmd.run 'nginx -s reload'
